<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 使用apply()自己手写一个辅助函数bind()
      function foo() {
        console.log(this);
      }

      var obj = {
        name: "why",
      };

      function bind(func, obj) {
        return function () {
          return func.apply(obj, arguments);
        };
      }

      var bar = bind(foo, obj);
      bar();
      bar();
      bar();

      // 使用Function.prototype.bind
      function foo() {
        console.log(this);
      }

      var obj = {
        name: "why",
      };

      var bar = foo.bind(obj);
      bar();
      bar();
      bar();
    </script>
  </body>
</html>
